package kr.co.sdsway2.web.client;

import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import kr.co.sdsway2.common.AbstractContraller;
import kr.co.sdsway2.service.AvatarService;
import kr.co.sdsway2.service.PollAswInfBeanService;
import kr.co.sdsway2.service.PollInfBeanService;
import kr.co.sdsway2.service.PollQstService;
import kr.co.sdsway2.service.PtInfBeanService;
import kr.co.sdsway2.vo.AvatarVO;
import kr.co.sdsway2.vo.PtInfVO;

import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class ScreenController extends AbstractContraller {

	//[TODO] ?? ajax 두번씩 호출되는 경우가 발생하여, 점수 계산이 두번씩 이루어지는 문제 발견 (원인찾기전까지 임시로 사용)
	private String beforePollId = "";

	@Resource(name="avatarService")
	AvatarService avatarService;

	@Resource(name="pollQstService")
	PollQstService pollQstService;
	
	@Resource(name="ptInfBeanService")
	PtInfBeanService ptInfBeanService;
	
	@Resource(name="pollInfBeanService")
	PollInfBeanService pollInfBeanService;

	@Resource(name="pollAswInfBeanService")
	PollAswInfBeanService pollAswInfBeanService;

	@RequestMapping("screen")
	public ModelAndView screen(HttpServletRequest req,
			HttpServletResponse res) throws Exception {
		ModelAndView mav = new ModelAndView("screen.jsp");
		return mav;
	}

	@RequestMapping("screenAjax")
	public void screenAjax(Model model, HttpServletRequest req){
		
//		debug(">>>>>	obj poll insert complete!");
//		debug(">>>>>	current PtInf : "+ ptInfBeanService.getList());
//		debug(">>>>>	current PollInf : "+ pollInfBeanService.getList());

		PtInfVO ptInfVO = ptInfBeanService.getPtInfBean(1);
		if(ptInfVO!=null){
			model.addAttribute("ptStatCd", ptInfVO.getPtStatCd());
			Integer pstPollId = ptInfVO.getPstPollId();
			if(pstPollId!=null && pstPollId>0){
				model.addAttribute("pollInfVO", pollInfBeanService.getPollInfBean(1,pstPollId));
				model.addAttribute("pollAswInfVOList", pollAswInfBeanService.getList(pstPollId));
			}
		}

		model.addAttribute("ptInfVO", ptInfVO);
		model.addAttribute("avatarVOList", avatarService.getList());
//		debug(">>>>> pollAswInfBeanService.getList() : "+ pollAswInfBeanService.getList());
		

//		HashMap<String, Object> pollQstMap = pollQstService.getPollQst();
//		if( pollQstMap!=null && pollQstMap.get("ptId")!=null ) model.addAttribute("pollQstMap", pollQstMap);
	}
	
	@RequestMapping("setScoreAjax")
	public void setScoreAjax(Model model, HttpServletRequest req) throws JsonProcessingException, IOException {
		String pollId = req.getParameter("pollId");
		if( beforePollId.equals(pollId) ) {
			debug("[TEMP] setScoreAjax - 2!!!!!");
			return ;
		}
		beforePollId = pollId;

		String avatarMapJSONString = req.getParameter("avatarMapJSONString");
		debug("[TEMP] avatarMapJSONString : " + avatarMapJSONString);
		ObjectMapper objectMapper = new ObjectMapper();
		JsonNode avatarMapJsonNode = objectMapper.readTree(avatarMapJSONString);
		List<AvatarVO> avatarVOList = avatarService.getList();
		JsonNode tempJsonNode;
		String pointText;
		AvatarVO avatarVO;

		if( avatarVOList != null ) {
			for(int i=0; i<avatarVOList.size(); i++) {
				avatarVO = avatarVOList.get(i);
				tempJsonNode = avatarMapJsonNode.get(avatarVO.getAvatarId());
				if( tempJsonNode != null ) {
					pointText = tempJsonNode.getTextValue();
					if( pointText != null ) avatarVO.setAvatarScore(avatarVO.getAvatarScore() + Integer.parseInt(pointText));
				}
			}
		}
		avatarService.sortAvatarVOList();

		//[TEMP] 디버그용
//		List<AvatarVO> avatarVOList = avatarService.getList();
//		debug("=====================================");
//		for(int i=0; i<avatarVOList.size(); i++) {
//			debug(i + " >>>>> " + avatarVOList.get(i));
//		}
//		debug("=====================================");
	}

	@RequestMapping("setPositionAjax")
	public void setPositionAjax(Model model, HttpServletRequest req) throws JsonProcessingException, IOException {
		int minPosX = (req.getParameter("minPosX")==null)? 0:Integer.parseInt(req.getParameter("minPosX"));
		int minPosY = (req.getParameter("minPosY")==null)? 0:Integer.parseInt(req.getParameter("minPosY"));
		int maxPosX = (req.getParameter("maxPosX")==null)? 0:Integer.parseInt(req.getParameter("maxPosX"));
		int maxPosY = (req.getParameter("maxPosY")==null)? 0:Integer.parseInt(req.getParameter("maxPosY"));
		avatarService.setPosition(minPosX, minPosY, maxPosX, maxPosY);
	}

}
